﻿using System;

namespace OpenWaves.Caching
{
    public static class CacheEntry
    {
        public static CacheEntry<T> FromValue<T>(T value, DateTime absoluteExpiration) where T : class
        {
            return new CacheEntry<T>(value, absoluteExpiration);
        }

        public static CacheEntry<T> FromValue<T>(T value, TimeSpan absoluteExpiration) where T : class
        {
            return new CacheEntry<T>(value, absoluteExpiration == TimeSpan.MaxValue ? DateTime.MaxValue : Time.Now + absoluteExpiration);
        }
    }

    [Serializable]
    public class CacheEntry<T> where T : class
    {
        public CacheEntry(T value, DateTime absoluteExpiration)
        {
            this.Value = value;
            this.AbsoluteExpiration = absoluteExpiration;
        }

        public CacheEntry(T value) : this(value, DateTime.MaxValue)
        {            
        }

        public DateTime AbsoluteExpiration { get; private set; }
        public T Value { get; private set; }

        public bool IsExpired
        {
            get { return this.AbsoluteExpiration < Time.Now; }
        }
    }
}